package com.tanhua.server;

import cn.hutool.cache.CacheUtil;
import cn.hutool.core.collection.CollUtil;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.exeception.BusinessException;
import com.tanhua.interceptor.UserHolder;
import com.tanhua.model.dimain.User;
import com.tanhua.model.dimain.UserInfo;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 杰哥不要啦~
 * @date 2021/10/31
 */
@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @Autowired
    private HuanXinTemplate template;

    @DubboReference
    private FriendApi friendApi;


    public UserInfoVo findUserInfoByHuanxin(String huanxinid) {

        //1、根据环信id查询用户
        User user =  userApi.findByhuanxin(huanxinid);
        //2、根据用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,vo);//copy同名同类型的属性
        //判断
        if(userInfo.getAge() != null) {
            vo.setAge(userInfo.getAge().toString());
        }
        return vo;
    }

    //添加好友
    public void contacts(Long friendId) {
        //1、将好友关系注册到环信
        Boolean aBoolean = template.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserid(),
                Constants.HX_USER_PREFIX + friendId);
        //判断是否添加成功
        if(!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        //2、如果注册成功，记录好友关系到mongodb
        friendApi.save(UserHolder.getUserid(),friendId);
    }

    //分页查询联系人
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1、调用API查询当前用户的好友数据 -- List<Friend>
        List<Friend> byUserId = friendApi.findByUserId(UserHolder.getUserid(), page, pagesize);//好友id结合
        //判断是否为空
        if (CollUtil.isEmpty(byUserId)){
            return new PageResult();
        }
        //2、提取数据列表中的好友id
        List<Long> fiendId = CollUtil.getFieldValues(byUserId, "fiendId", Long.class);
        //3、调用UserInfoAPI查询好友的用户详情
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(keyword);//设置查询条件 名字
        Map<Long,UserInfo> map = userInfoApi.finByIds(fiendId, userInfo);
        //4、构造VO对象
        List<ContactVo> list = new ArrayList<>();
        for (Friend friend : byUserId) {
            UserInfo info = map.get(friend.getFriendId());
            if (info == null){
                ContactVo vo = ContactVo.init(info);
                list.add(vo);
            }
        }
        return new  PageResult(page,pagesize,0l,list);
    }
}
